﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Windows;
using mitasoft.Books.Client.Desktop.Properties;
using mitasoft.Books.Client.Desktop.Helpers;
using System.Reflection;

namespace mitasoft.Books.Client.Desktop.Core
{
    internal class CategoryFinder
    {
        private CategoryDescriptorCollection category;

        public CategoryFinder()
        {
            //search if exists
            string startPath = System.Windows.Forms.Application.StartupPath;
            string path = Path.Combine(startPath, Settings.Default.CategoryDescriptorFileName);

            if (File.Exists(path))
            {
                using (var reader = new StreamReader(path))
                {
                    category = XMLHelper.Deserialize<CategoryDescriptorCollection>(reader.BaseStream);
                }
            }
            else
            {
                //create default settings
                category = GetDefaultSearchSettings();
                using (var reader = new StreamWriter(path))
                {
                    XMLHelper.Serialize<CategoryDescriptorCollection>(category, reader.BaseStream);
                }
            }
        }

        public List<string> GetCategoryList()
        {
            if (category != null)
            {
                return (from c in category
                        select c.CategoryName).ToList();
            }

            return new List<string>();
        }

        public string GetCategory(string filename)
        {
            filename = filename.Replace('.', ' ').Replace('_', ' ').Replace('-', ' ').ToLower();
            //string machedCategory = string.Format("---{0}---", filename);
            string machedCategory = "Other";


            foreach (var categDescriptor in this.category)
            {
                if (Regex.Match(filename, categDescriptor.SearchRegexList.ToLower()).Success)
                {
                    machedCategory = categDescriptor.CategoryName;
                    break;
                }
            }

            return machedCategory;
        }

        private CategoryDescriptorCollection GetDefaultSearchSettings()
        {
            CategoryDescriptorCollection searchSettings = new CategoryDescriptorCollection();

            searchSettings.Add(new CategoryDescriptor
            {
                CategoryName = "Other",
                SearchRegexList = "-"
            });

            searchSettings.Add(new CategoryDescriptor
            {
                CategoryName = "Expression",
                SearchRegexList = "(expression)+|(blend)+"
            });

            searchSettings.Add(new CategoryDescriptor
            {
                CategoryName = "WF",
                SearchRegexList = "(workflow)+"
            });

            searchSettings.Add(new CategoryDescriptor
            {
                CategoryName = "Javascript",
                SearchRegexList = "(javascript)+"
            });

            searchSettings.Add(new CategoryDescriptor
            {
                CategoryName = "CRM",
                SearchRegexList = "(crm)+"
            });

            searchSettings.Add(new CategoryDescriptor
            {
                CategoryName = "Linq",
                SearchRegexList = "(linq)+"
            });

            searchSettings.Add(new CategoryDescriptor
            {
                CategoryName = "ADO&SQL",
                SearchRegexList = "(ado)+|(entity)+|(sql)+"
            });

            searchSettings.Add(new CategoryDescriptor
            {
                CategoryName = "Oracle",
                SearchRegexList = "(oracle)+"
            });

            searchSettings.Add(new CategoryDescriptor
            {
                CategoryName = "UML",
                SearchRegexList = "(uml)+"
            });

            searchSettings.Add(new CategoryDescriptor
            {
                CategoryName = "Photoshop",
                SearchRegexList = "(photoshop)+"
            });

            searchSettings.Add(new CategoryDescriptor
            {
                CategoryName = "Sharepoint",
                SearchRegexList = "(sharepoint)+"
            });

            searchSettings.Add(new CategoryDescriptor
            {
                CategoryName = "VisualStudio",
                SearchRegexList = "(visual studio)+"
            });

            searchSettings.Add(new CategoryDescriptor
            {
                CategoryName = "Architecture",
                SearchRegexList = "(architecture)+"
            });

            searchSettings.Add(new CategoryDescriptor
            {
                CategoryName = "Patterns&Guidelines",
                SearchRegexList = "(guidline)+|(pattern)+|(refactor)+"
            });

            searchSettings.Add(new CategoryDescriptor
            {
                CategoryName = "Android",
                SearchRegexList = "(android)+"
            });

            searchSettings.Add(new CategoryDescriptor
            {
                CategoryName = "Agile",
                SearchRegexList = "(agile)+"
            });

            searchSettings.Add(new CategoryDescriptor
            {
                CategoryName = "WPF",
                SearchRegexList = "(wpf)+|(windows presentation)+"
            });

            searchSettings.Add(new CategoryDescriptor
            {
                CategoryName = "C#",
                SearchRegexList = "(sharp)+|(csharp)+|(clr)+|(c#)+"
            });

            searchSettings.Add(new CategoryDescriptor
            {
                CategoryName = "WCF",
                SearchRegexList = "(wcf)+|(windows communication)+"
            });

            searchSettings.Add(new CategoryDescriptor
            {
                CategoryName = "Silverlight",
                SearchRegexList = "(silverlight)+|(silver)+"
            });

            searchSettings.Add(new CategoryDescriptor
            {
                CategoryName = "Services",
                SearchRegexList = "(service)+|(services)+"
            });

            searchSettings.Add(new CategoryDescriptor
            {
                CategoryName = "ASP.NET",
                SearchRegexList = "(asp)+|(562)+"
            });

            return searchSettings;
        }
    }
}
